package com.tree.backend.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tree.backend.common.Result;
import com.tree.backend.service.ICostRecordService;
import com.tree.backend.entity.dto.costRecord.*;
import com.tree.backend.entity.vo.costRecord.CostRecordVO;
import com.tree.backend.entity.vo.costRecord.CostAnalysisVO;
import com.tree.backend.service.IUserService;
import lombok.RequiredArgsConstructor;

import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * <p>
 * 成本记录明细表 前端控制器
 * </p>
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/cost-records")
public class CostRecordController {

    private final ICostRecordService costRecordService;
    private final IUserService userService;

    /**
     * 获取成本记录列表
     */
    @GetMapping
    public Result<Page<CostRecordVO>> getCostRecords(CostRecordQueryDTO queryDTO) {
        Page<CostRecordVO> page = costRecordService.getCostRecords(queryDTO);
        return Result.success(page);
    }

    /**
     * 获取成本记录详情
     */
    @GetMapping("/{id}")
    public Result<CostRecordVO> getCostRecordById(@PathVariable Long id) {
        CostRecordVO costRecord = costRecordService.getCostRecordById(id);
        if (costRecord == null) {
            return Result.error(404, "成本记录不存在");
        }
        return Result.success(costRecord);
    }

    /**
     * 新增成本记录
     */
    @PostMapping
    public Result<CostRecordVO> addCostRecord(@RequestBody CostRecordCreateDTO createDTO) {
        CostRecordVO costRecord = costRecordService.addCostRecord(createDTO);
        return Result.success(costRecord);
    }

    /**
     * 更新成本记录
     */
    @PutMapping("/{id}")
    public Result<CostRecordVO> updateCostRecord(@PathVariable Long id, @RequestBody CostRecordUpdateDTO updateDTO) {
        CostRecordVO costRecord = costRecordService.updateCostRecord(id, updateDTO);
        if (costRecord == null) {
            return Result.error(404, "成本记录不存在");
        }
        return Result.success(costRecord);
    }

    /**
     * 删除成本记录
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteCostRecord(@PathVariable Long id) {
        boolean success = costRecordService.deleteCostRecord(id);
        if (!success) {
            return Result.error(404, "成本记录不存在或删除失败");
        }
        return Result.success(true);
    }

    /**
     * 更新成本记录状态
     */
    @PutMapping("/{id}/status")
    public Result<CostRecordVO> updateCostRecordStatus(@PathVariable Long id, @RequestBody CostRecordStatusDTO statusDTO) {
        CostRecordVO costRecord = costRecordService.updateCostRecordStatus(id, statusDTO);
        if (costRecord == null) {
            return Result.error(404, "成本记录不存在");
        }
        return Result.success(costRecord);
    }

    /**
     * 审批成本记录
     * @param id 成本记录ID
     * @param approveDTO 审批信息，包含审批状态和审批意见
     * @return 审批后的成本记录信息
     */
    @PutMapping("/{id}/approve")
    public Result<CostRecordVO> approveCostRecord(@PathVariable Long id, @RequestBody CostRecordApproveDTO approveDTO) {
        // 参数校验
        if (approveDTO.getApprovalStatus() == null) {
            return Result.error(400, "审批状态不能为空");
        }
        
        // 从JWT获取当前登录用户名
        String currentUsername = userService.getUserByUserName();
        if (currentUsername == null) {
            return Result.error(401, "用户未登录或登录已过期");
        }
        
        CostRecordVO costRecord = costRecordService.approveCostRecord(id, approveDTO, currentUsername);
        if (costRecord == null) {
            return Result.error(404, "成本记录不存在");
        }
        return Result.success(costRecord);
    }

    /**
     * 获取成本分析数据
     */
    @GetMapping("/analysis")
    public Result<List<CostAnalysisVO>> getCostAnalysis(CostAnalysisQueryDTO queryDTO) {
        List<CostAnalysisVO> analysisList = costRecordService.getCostAnalysis(queryDTO);
        return Result.success(analysisList);
    }

    /**
     * 搜索成本记录
     */
    @GetMapping("/search")
    public Result<Page<CostRecordVO>> searchCostRecords(CostRecordQueryDTO queryDTO) {
        Page<CostRecordVO> page = costRecordService.searchCostRecords(queryDTO);
        return Result.success(page);
    }
}
